import com.wego.bean.PageBean;
import com.wego.bean.ResultBean;
import com.wego.converter.${Entity}Converter;
import com.wego.domain.details.${Entity}Details;
import com.wego.domain.dto.${Entity}DTO;
import com.wego.domain.entity.${Entity};
import com.wego.domain.excel.${Entity}Excel;
import com.wego.domain.query.${Entity}Query;
import com.wego.domain.vo.${Entity}VO;
import com.wego.ex.GlobalException;
import com.wego.service.${Entity}Service;
import com.wego.utils.excel.EasyExcelListener;
import com.wego.utils.excel.EasyExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * ${desc}(tb_${obj})表控制层
 *
 */
@RestController
@RequestMapping("/admin/${obj}")
public class ${Entity}Controller {
    @Autowired
    private ${Entity}Service ${obj}Service;
    @Autowired
    private ${Entity}Converter ${obj}Converter;


    @PostMapping("/v1/page")
    public ResultBean<PageBean<${Entity}VO>> page(@RequestBody ${Entity}Query ${obj}Query) {
        final PageBean<${Entity}> ${obj}PageBean = ${obj}Service.getPage(${obj}Query);
        final PageBean<${Entity}VO> pageBean = ${obj}Converter.entityPageBean2voPageBean(${obj}PageBean);
        return ResultBean.success(pageBean);
    }

    @GetMapping("/v1/details/{id}")
    public ResultBean<${Entity}Details> getDetails(@PathVariable("id") Integer id) {
        final ${Entity} ${obj} = ${obj}Service.getById(id);
        final ${Entity}Details ${obj}Details = ${obj}Converter.${obj}2${obj}Details(${obj});
        return ResultBean.success(${obj}Details);
    }

    @PutMapping("/v1/update")
    public ResultBean<Void> update(@RequestBody ${Entity}DTO ${obj}DTO) {
        final ${Entity} ${obj} = ${obj}Converter.dto2entity(${obj}DTO);
        ${obj}Service.insertOrUpdate(${obj});
        return ResultBean.success("修改成功!", null);
    }

    @PostMapping("/v1/insert")
    public ResultBean<Void> insert(@RequestBody ${Entity}DTO ${obj}DTO) {
        final ${Entity} ${obj} = ${obj}Converter.dto2entity(${obj}DTO);
        ${obj}Service.insertOrUpdate(${obj});
        return ResultBean.success("添加成功!", null);
    }

    @DeleteMapping("/v1/delete/{id}")
    public ResultBean<Void> delete(@PathVariable("id") Integer id) {
        ${obj}Service.deleteById(id);
        return ResultBean.success("删除成功!", null);
    }

    //3,5,88,8
    @DeleteMapping("/v1/batchDelete/{ids}")
    public ResultBean<Void> batchDelete(@PathVariable("ids") String ids) {
        ${obj}Service.deleteByIds(ids);
        return ResultBean.success("批量删除成功!", null);
    }

    @PostMapping("/v1/export")
    public void exportData(HttpServletResponse response, @RequestBody ${Entity}Query ${obj}Query) {
        final PageBean<${Entity}> pageBean = ${obj}Service.getPage(${obj}Query);

        final List<${Entity}Excel> list = ${obj}Converter.entityList2excelList(pageBean.getRecords());
        try {
            EasyExcelUtil.writeWithSheetsWeb(response, "${obj}列表")
                    .writeModel(${Entity}Excel.class, list, "${obj}")
                    .finish();
        } catch (IOException e) {
            throw new GlobalException("${desc}信息导出失败！");
        }
    }

    @PostMapping("/v1/import")
    public ResultBean<Void> importData(@RequestPart(value = "file", required = true) MultipartFile file) {
        try (InputStream is = file.getInputStream()) {
            final EasyExcelListener<${Entity}Excel> listener = new EasyExcelListener<${Entity}Excel>() {
                @Override
                protected void exec(List<${Entity}Excel> list) {
                    final List<${Entity}> ${obj}List = ${obj}Converter.excelList2entityList(list);
                    ${obj}Service.batchInsert(${obj}List);
                }
            };
            EasyExcelUtil.asyncReadModel(is, listener, ${Entity}Excel.class);
        } catch (IOException e) {
            throw new GlobalException("${desc}信息导入失败！");
        }
        return ResultBean.success("导入成功!", null);
    }
}
